#include "scroll.h"

/*------------------------------------------------------------------------------
	Constructor
------------------------------------------------------------------------------*/
CScroll::CScroll()
{
	int i;	
	for(i=0;i<MAX_SCROLLS;i++)
	{
		m_tScroll[i].m_fVelocX=0.005;
		m_tScroll[i].m_fVelocY=0;
		m_tScroll[i].m_pTextura=NULL;
		m_tScroll[i].m_fRollX=0;
		m_tScroll[i].m_fRollY=0;
		m_tScroll[i].m_fDivisionesX=1;
		m_tScroll[i].m_fDivisionesY=1;
		m_tScroll[i].m_bAutomatico=FALSE;
		m_tScroll[i].m_bMascaraAsignada=FALSE;		
		m_tScroll[i].m_bMover=FALSE;
		m_tScroll[i].m_bEscalaColor=FALSE;
		m_tScroll[i].m_aColor[0]=1.0f;
		m_tScroll[i].m_aColor[1]=1.0f;
		m_tScroll[i].m_aColor[2]=1.0f;	
		m_tScroll[i].m_aColorAbajo[0]=1.0f;
		m_tScroll[i].m_aColorAbajo[1]=1.0f;
		m_tScroll[i].m_aColorAbajo[2]=1.0f;	
		m_tScroll[i].m_aColorArriba[0]=1.0f;
		m_tScroll[i].m_aColorArriba[1]=1.0f;
		m_tScroll[i].m_aColorArriba[2]=1.0f;	
	}
	m_pPantalla=NULL;
	m_iNumScrolls=0;
}

/*------------------------------------------------------------------------------
	Destructor
------------------------------------------------------------------------------*/
CScroll::~CScroll()
{
}

/*------------------------------------------------------------------------------
	Initializes object
------------------------------------------------------------------------------*/
Bool CScroll::Iniciar(int numScroll,CTextura *pTextura,CPantalla *pPantalla,Bool Auto,float fVelocX,float fVelocY,float DivX,float DivY)
{
	if(!pTextura || !pPantalla || m_iNumScrolls>=MAX_SCROLLS)
		return FALSE;
		
	m_tScroll[numScroll].m_fVelocX=fVelocX;
	m_tScroll[numScroll].m_fVelocY=fVelocY;	
	m_tScroll[numScroll].m_pTextura=pTextura;
	m_tScroll[numScroll].m_bAutomatico=Auto;
	m_tScroll[numScroll].m_fDivisionesX=DivX;
	m_tScroll[numScroll].m_fDivisionesY=DivY;
	m_pPantalla=pPantalla;	
	m_iNumScrolls++;
	return TRUE;
}

/*------------------------------------------------------------------------------
	Initializes object
------------------------------------------------------------------------------*/
Bool CScroll::Iniciar(int numScroll,CPantalla *pPantalla,float aColorArriba[3],float aColorAbajo[3])
{
	if(!pPantalla || m_iNumScrolls>=MAX_SCROLLS)
		return FALSE;
		
	m_pPantalla=pPantalla;	
	
	m_tScroll[numScroll].m_bEscalaColor=TRUE;
	m_tScroll[numScroll].m_aColorAbajo[0]=aColorAbajo[0];
	m_tScroll[numScroll].m_aColorAbajo[1]=aColorAbajo[1];
	m_tScroll[numScroll].m_aColorAbajo[2]=aColorAbajo[2];	
	m_tScroll[numScroll].m_aColorArriba[0]=aColorArriba[0];
	m_tScroll[numScroll].m_aColorArriba[1]=aColorArriba[1];
	m_tScroll[numScroll].m_aColorArriba[2]=aColorArriba[2];
	m_iNumScrolls++;
}

/*------------------------------------------------------------------------------
	Memory free method
------------------------------------------------------------------------------*/
void CScroll::Liberar()
{
	int i;
	m_pPantalla=NULL;
	for(i=0;i<MAX_SCROLLS;i++)	
		m_tScroll[i].m_pTextura=NULL;

	printf( "- Scroll Liberado\n");
}

/*------------------------------------------------------------------------------
	Draws the parallax scroll
------------------------------------------------------------------------------*/
void CScroll::Dibujar()
{
	int i;
	for(i=m_iNumScrolls-1;i>=0;i--)				
	 	if(!m_tScroll[i].m_bEscalaColor)
 			DibujarNormal(i);
 		else 	
			DibujarEscalaColor(i);
}

/*------------------------------------------------------------------------------
	Draws scroll
------------------------------------------------------------------------------*/
void CScroll::DibujarNormal(int numScroll)
{
	float avanceX;
	
	if(m_pPantalla){		
		if(m_tScroll[numScroll].m_fX!=m_pPantalla->getCamX() || m_tScroll[numScroll].m_fY!=m_pPantalla->getCamY())
		{
			if(m_tScroll[numScroll].m_fX<m_pPantalla->getCamX())
				avanceX=m_tScroll[numScroll].m_fVelocX;
			else
				avanceX=-m_tScroll[numScroll].m_fVelocX;
				
			m_tScroll[numScroll].m_bMover=TRUE;				
			m_tScroll[numScroll].m_fX=m_pPantalla->getCamX();
			m_tScroll[numScroll].m_fY=m_pPantalla->getCamY();
		}
	}	

	if(!m_tScroll[numScroll].m_pTextura)
		return;
		
	if(m_tScroll[numScroll].m_bAutomatico)
		avanceX=m_tScroll[numScroll].m_fVelocX;

    if (m_tScroll[numScroll].m_bAutomatico || m_tScroll[numScroll].m_bMover)
    {
		m_tScroll[numScroll].m_bMover=FALSE;
		m_tScroll[numScroll].m_fRollX+=avanceX;
		m_tScroll[numScroll].m_fRollY+=m_tScroll[numScroll].m_fVelocY;	    
    }

	glPushMatrix(); 
	glLoadIdentity();
	glEnable( GL_BLEND );
	glDisable( GL_DEPTH_TEST );
	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0.1f); 
	
	glColor3f(m_tScroll[numScroll].m_aColor[0],m_tScroll[numScroll].m_aColor[1],m_tScroll[numScroll].m_aColor[2]);
		
	m_tScroll[numScroll].m_pTextura->Bind();	
	//glBindTexture( GL_TEXTURE_2D, 0 );
	
    glBegin( GL_TRIANGLES );     		
	    	glTexCoord2f( m_tScroll[numScroll].m_fRollX +0.0f,  m_tScroll[numScroll].m_fRollY +m_tScroll[numScroll].m_fDivisionesY ); 
      		glVertex3f( -1.1f+m_tScroll[numScroll].m_fX, -1.1f+m_tScroll[numScroll].m_fY,  -1.0f );      
      		glTexCoord2f( m_tScroll[numScroll].m_fRollX +m_tScroll[numScroll].m_fDivisionesX,  m_tScroll[numScroll].m_fRollY +m_tScroll[numScroll].m_fDivisionesY ); 
      		glVertex3f(  1.1f+m_tScroll[numScroll].m_fX, -1.1f+m_tScroll[numScroll].m_fY,  -1.0f );      
      		glTexCoord2f( m_tScroll[numScroll].m_fRollX +m_tScroll[numScroll].m_fDivisionesX,  m_tScroll[numScroll].m_fRollY +0.0f ); 
      		glVertex3f(  1.1f+m_tScroll[numScroll].m_fX,  1.1f+m_tScroll[numScroll].m_fY,  -1.0f );

			// Segundo triangulo
	    	glTexCoord2f( m_tScroll[numScroll].m_fRollX +0.0f,  m_tScroll[numScroll].m_fRollY +m_tScroll[numScroll].m_fDivisionesY ); 
      		glVertex3f( -1.1f+m_tScroll[numScroll].m_fX, -1.1f+m_tScroll[numScroll].m_fY,  -1.0f );
      		glTexCoord2f( m_tScroll[numScroll].m_fRollX +m_tScroll[numScroll].m_fDivisionesX,  m_tScroll[numScroll].m_fRollY +0.0f ); 
      		glVertex3f(  1.1f+m_tScroll[numScroll].m_fX,  1.1f+m_tScroll[numScroll].m_fY,  -1.0f );      
      		glTexCoord2f( m_tScroll[numScroll].m_fRollX +0.0f,  m_tScroll[numScroll].m_fRollY +0.0f ); 
      		glVertex3f( -1.1f+m_tScroll[numScroll].m_fX,  1.1f+m_tScroll[numScroll].m_fY,  -1.0f );      
      		
    	glEnd( );

	glDisable(GL_ALPHA_TEST);
    glEnable( GL_DEPTH_TEST );
    glDisable( GL_BLEND );    
    glPopMatrix();   
    glColor3f(1,1,1); 	
}
/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void CScroll::DibujarEscalaColor(int numScroll)
{
	m_tScroll[numScroll].m_fX=m_pPantalla->getCamX();
	m_tScroll[numScroll].m_fY=m_pPantalla->getCamY();
	
	glPushMatrix(); 
	glLoadIdentity();
	glEnable( GL_BLEND );
	glDisable( GL_DEPTH_TEST );
	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0.1f); 
		
	glBindTexture( GL_TEXTURE_2D, 0 );
	
    glBegin( GL_TRIANGLES );     		
    	glColor3f(m_tScroll[numScroll].m_aColorAbajo[0],m_tScroll[numScroll].m_aColorAbajo[1],m_tScroll[numScroll].m_aColorAbajo[2]);
    	glVertex3f( -1.1f+m_tScroll[numScroll].m_fX, -1.1f+m_tScroll[numScroll].m_fY,  -1.0f );      
    	glVertex3f(  1.1f+m_tScroll[numScroll].m_fX, -1.1f+m_tScroll[numScroll].m_fY,  -1.0f );          	
    	glColor3f(m_tScroll[numScroll].m_aColorArriba[0],m_tScroll[numScroll].m_aColorArriba[1],m_tScroll[numScroll].m_aColorArriba[2]);
    	glVertex3f(  1.1f+m_tScroll[numScroll].m_fX,  1.1f+m_tScroll[numScroll].m_fY,  -1.0f );

		// Segundo triangulo		
    	glColor3f(m_tScroll[numScroll].m_aColorAbajo[0],m_tScroll[numScroll].m_aColorAbajo[1],m_tScroll[numScroll].m_aColorAbajo[2]);
    	glVertex3f( -1.1f+m_tScroll[numScroll].m_fX, -1.1f+m_tScroll[numScroll].m_fY,  -1.0f );
    	glColor3f(m_tScroll[numScroll].m_aColorArriba[0],m_tScroll[numScroll].m_aColorArriba[1],m_tScroll[numScroll].m_aColorArriba[2]);
    	glVertex3f(  1.1f+m_tScroll[numScroll].m_fX,  1.1f+m_tScroll[numScroll].m_fY,  -1.0f );      
    	glVertex3f( -1.1f+m_tScroll[numScroll].m_fX,  1.1f+m_tScroll[numScroll].m_fY,  -1.0f );            		
    glEnd( );

	glDisable(GL_ALPHA_TEST);
    glEnable( GL_DEPTH_TEST );
    glDisable( GL_BLEND );    
    glPopMatrix();   
    glColor3f(1,1,1); 	
}
/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void CScroll::Actualizar(int numScroll)
{
	m_tScroll[numScroll].m_fRollX+=m_tScroll[numScroll].m_fVelocX;
	m_tScroll[numScroll].m_fRollY+=m_tScroll[numScroll].m_fVelocY;	
}

